package com.foreverwin.jzymes.eap.Listener;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.domain.CncReqLog;
import com.foreverwin.jzymes.common.service.ICncReqLogAdviceService;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.framework.annotation.MessageLog;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.cnc.service.ICncSfcPassAdviceService;
import org.apache.activemq.command.ActiveMQTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class CncListener {

    private Logger logger = LoggerFactory.getLogger( CncListener.class );

    @Autowired
    ICncSfcPassAdviceService cncSfcPassAdviceService;
    @Autowired
    ICncReqLogAdviceService cncReqLogAdviceService;
    @Autowired
    JmsMessagingTemplate jmsTemplate;

    @JmsListener( destination = "MES_CNC1_INFRA" )
    @MessageLog
    public void sfcPassByCarrierFromQueue( String cncReqLogJson ) throws IOException {
        logger.info( "接收到一夹自动线过站信息: " + cncReqLogJson );
        try{
            CncReqLog cncReqLog = ObjectMapperUtil.readValueForUnderscore( cncReqLogJson, CncReqLog.class );
            cncReqLog.setCarrier( CarrierUtil.buildCarrier( cncReqLog.getSite(), cncReqLog.getCarrier() ) );
            cncSfcPassAdviceService.sfcPassByCarrier( cncReqLog );

            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", cncReqLog.getSite() );
            eventData.put( "RESRCE", cncReqLog.getResrce() );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "CNC_CARRIER_OUT" );
            appEvent.setData( eventData );
            ActiveMQTopic topic = new ActiveMQTopic( "MES_APP_EVENT_INFRA" );
            jmsTemplate.convertAndSend( topic, appEvent );

            logger.info( "一夹自动线过站信息处理完成: " + cncReqLogJson );
        }catch ( Exception e ){
            logger.error( "一夹自动线过站信息处理失败: " + e.getMessage() + "，一夹自动线过站信息: " + cncReqLogJson );
            throw e;
        }
    }

    @JmsListener( destination = "MES_CNC_NG_INFRA" )
    @MessageLog
    public void receiveNgCarrierFromQueue( String cncReqLogJson ) throws IOException {
        logger.info( "接收到一夹自动线不良本体数量信息: " + cncReqLogJson );
        Map<String,Object> map;
        try{
            map = ObjectMapperUtil.readValueForUnderscore( cncReqLogJson, HashMap.class );
            String site = (String) map.get( "SITE" );
            String resource = (String) map.get( "RESRCE" );
            Long ngQty = Long.valueOf( map.get( "NG_NUM" ) + "" );
            Date sendTime = DateUtil.parse((String) map.get( "SEND_TIME" ));
            cncSfcPassAdviceService.receiveNgCarrierFromQueue( site, resource, ngQty, sendTime );
            logger.info( "一夹自动线不良本体数量信息处理完成: " + cncReqLogJson );
        }catch ( Exception e ){
            logger.error( "一夹自动线不良本体数量信息处理失败: " + e.getMessage() + "，一夹自动线不良本体数量信息: " + cncReqLogJson );
            throw e;
        }
    }

}
